/**
  ******************************************************************************
  * @file    ring_buffer.c
  * @author  Firmware Worker
  * @brief   ring buffer implementation
  *          ring buffer reference: https://www.cnblogs.com/zengzy/p/5139582.html
  *
  * \license The MIT License (MIT)
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  * 
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
  * 
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include<stdint.h>
#include "ring_buffer.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
 * @brief Put data to ring buffer.
 * 
 * @param pRingBuf ring buffer pointer
 * @param data data buffer to put
 * @param len put data length
 * @return int16_t put result
 */
int16_t iPutDataToBuf(tRingBuf* pRingBuf, uint8_t* data, int16_t len)
{
  int16_t result = RING_BUF_OK;

  for (int16_t i = 0; i < len; i++)
  {
    if (((pRingBuf->in + 1) % pRingBuf->size) != pRingBuf->out)
    {
      pRingBuf->buf[pRingBuf->in++] = data[i];
      pRingBuf->in %= pRingBuf->size;
    }
    else
    {
#if RECORD_LOST_NUM
      pRingBuf->lostNum++;
#endif
      result = RING_BUF_OVERFLOW;
    }
  }

  return result;
}

/**
 * @brief Get data from ring buffer.
 * 
 * @param pRingBuf ring buffer pointer
 * @param data got data buffer
 * @param len data length want to get
 * @return int16_t got data length
 */
int16_t iGetDataFromBuf(tRingBuf* pRingBuf, uint8_t* data, int16_t len)
{
  int16_t i;

  for (i = 0; i < len; i++)
  {
    if (pRingBuf->in != pRingBuf->out)
    {
      data[i] = pRingBuf->buf[pRingBuf->out++];
      pRingBuf->out %= pRingBuf->size;
    }
    else
    {
      break;
    }
  }

  return i;
}
